﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercice3
    {
    public class Hour
        {
        public Hour(Int32 h = 12, Int32 m = 0, Int32 s = 0)
            {
            if (!isHOk(h)) { h = 12; }
            if (!isMSOk(m)) { m = 0; }
            if (!isMSOk(s)) { s = 0; }
            this.m_h = h;
            this.m_m = m;
            this.m_s = s;
            this.check();
            }

        public void display()
            {
            System.Console.WriteLine("{0:0#}:{1:0#}:{2:0#}", this.m_h, this.m_m, this.m_s);
            }

        public override string ToString()
            {
            StringBuilder str = new StringBuilder();
            str.Append(this.m_h);
            str.Append(":");
            str.Append(this.m_m);
            str.Append(":");
            str.Append(this.m_s);
            return str.ToString();
            }

        public void add(Hour hour)
            {
            this.m_h += hour.h;
            this.m_m += hour.m;
            this.m_s += hour.s;
            this.check();
            }

        public void adjust(Int32 h, Int32 m, Int32 s)
            {
            if (!isHOk(h)) { h = 12; }
            if (!isMSOk(m)) { m = 0; }
            if (!isMSOk(s)) { s = 0; }

            this.m_h = h;
            this.m_m = m;
            this.m_s = s;
            this.check();
            }

        public void incrementMinutes(Int32 m)
            {
            this.m_m += m;
            this.check();
            }

        public void incrementSeconds(Int32 s)
            {
            this.m_s += s;
            this.check();
            }

        public Int32 h
            {
            get { return this.m_h; }
            }

        public Int32 m
            {
            get { return this.m_m; }
            }

        public Int32 s
            {
            get { return this.m_s; }
            }

        private void check()
            {
            Int32 ts = (Int32)(this.m_s % 60);
            if (this.m_s >= 60)
                {
                Int32 t = (Int32)(this.m_s / 60);
                this.m_s = ts;
                this.m_m += t;
                }

            ts = (Int32)(this.m_m % 60);
            if (this.m_m >= 60)
                {
                Int32 t = (Int32)(this.m_m / 60);
                this.m_m = ts;
                this.m_h += t;
                }

            if (this.m_h >= 24)
                {
                this.m_h = (Int32)(this.m_h % 24);
                }
            }

        private static bool isHOk(Int32 h)
            {
            return h >= 0 && h < 24;
            }

        private static bool isMSOk(Int32 ms)
            {
            return ms >= 0 && ms < 60;
            }

        private Int32 m_h;
        private Int32 m_m;
        private Int32 m_s;
        }
    }
